{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "# Quantum Superposition with Classiq\n",
    "\n",
    "Superposition is a key concept in all quantum algorithms. In this tutorial, we show how to create an equal superposition on 3 qubits, using the Hadamard transform. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "execution": {
     "iopub.execute_input": "2024-05-07T13:20:29.010252Z",
     "iopub.status.busy": "2024-05-07T13:20:29.006229Z",
     "iopub.status.idle": "2024-05-07T13:20:36.166576Z",
     "shell.execute_reply": "2024-05-07T13:20:36.165890Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Opening: https://platform.classiq.io/circuit/bdc37c77-6432-4f51-8c6c-f5a2d2263fde?version=0.41.0.dev39%2B79c8fd0855\n"
     ]
    }
   ],
   "source": [
    "from classiq import *\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def my_hadamard_transform(reg_a: QArray[QBit]) -> None:\n",
    "    apply_to_all(H, reg_a)\n",
    "\n",
    "\n",
    "@qfunc\n",
    "def main(register_a: Output[QArray[QBit]]) -> None:\n",
    "    allocate(3, register_a)\n",
    "    my_hadamard_transform(register_a)\n",
    "\n",
    "\n",
    "model = create_model(main)\n",
    "write_qmod(model, \"equal_superposition_3_qubits\")\n",
    "\n",
    "\n",
    "qprog = synthesize(model)\n",
    "\n",
    "circuit = QuantumProgram.from_qprog(qprog)\n",
    "\n",
    "circuit.show()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## Mathematical Background\n",
    "\n",
    "\n",
    "The 2x2 Hadamard matrix \\( H \\) is used to transform a single qubit's state. It is defined as:\n",
    "$$\n",
    "H = \\frac{1}{\\sqrt{2}} \\begin{bmatrix} 1 & 1 \\\\ 1 & -1 \\end{bmatrix}\n",
    "$$\n",
    "\n",
    "**Application to 3 Qubits**: Applying the Hadamard transform \\( H \\) to each of three qubits initially in the state $|000\\rangle$ results in the superposition of all possible states. The combined operation for three qubits is the tensor product $H \\otimes H \\otimes H$:\n",
    "$$\n",
    "(H \\otimes H \\otimes H) |000\\rangle = \\frac{1}{\\sqrt{8}} (|000\\rangle + |001\\rangle + |010\\rangle + |011\\rangle + |100\\rangle + |101\\rangle + |110\\rangle + |111\\rangle)\n",
    "$$\n",
    "This represents an equal superposition of all eight possible states of the three qubits.\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
